# load File.expand_path('../../helpers/helpers.rb', __dir__)
module DengHaiLightTools
  def self.edit_attr
    obj_list_attr = []
    obj_list = []
    objs = Sketchup.active_model.selection.grep(Sketchup::ComponentInstance)
    objs.each do |obj|
      t = helpers_get_attribute(obj) || nil
      next unless t

      obj_list_attr << t
      obj_list << obj
    end

    if obj_list.empty?
      UI.messagebox("未选择灯具！\n注意：只有从灯库导入的灯具才能生效。\n修改和炸开灯具也会导致灯具失效。")
      return
    end

    more_light_edit = UI.messagebox("同时修改 #{obj_list.length} 个灯具吗？", MB_YESNO) if obj_list.length > 1
    return unless more_light_edit == 6

    html_dialog = helpers_create_html_dialog('edit_light', '修改灯具', 'edit_light.html', 400, 400, false)

    html_dialog.add_action_callback('IwantGetLightAttr') do |_action_context|
      html_dialog.execute_script("get_light_info_form_ruby('#{obj_list_attr.to_json}')")
    end

    html_dialog.add_action_callback('IwantEditLightsAttr') do |_action_context, redata|
      obj_list.each do |obj|
        helpers_set_attribute_arry(obj, redata)
      end
      html_dialog.close
    end

    # IwantGetLightAttr
    html_dialog.show_modal
  end
end
